﻿Public Class frmOrdenPagoListado

    Private Sub frmVentasListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        fechaInicio.Value = DateTime.Now.AddDays(-30)
        fechaFin.Value = DateTime.Now
        dgVentas.AutoGenerateColumns = False
        CargarOrdenPago()

    End Sub

    Dim controller As New ControllerSigeco.controller_compras()

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(ByVal value As Integer)
            _codProveedor = value
        End Set
    End Property

    Private m_frmVentas As frmOrdenPago
    Public Property frmVentas() As frmOrdenPago
        Get
            Return m_frmVentas
        End Get
        Set(ByVal value As frmOrdenPago)
            m_frmVentas = value
        End Set
    End Property

    Private Sub CargarOrdenPago()

        Dim dt As New DataTable
        dt = controller.get_OrdenPago_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, _
                                    txtProveedor.Text, cboEstado.SelectedValue, txtNroComprobante.Text, "", codProveedor)
        dgVentas.DataSource = dt

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgVentas.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgVentas.SelectedRows(0).Cells(0).Value

                If frmVentas Is Nothing Then

                    frmVentas = New frmOrdenPago
                    frmVentas.codOrdenPago = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmVentas.ShowDialog()
                    CargarOrdenPago()
                    frmVentas.Dispose()
                    frmVentas = Nothing
                    'End If
                Else
                    frmVentas.codOrdenPago = cod
                    frmVentas.ShowDialog()
                    CargarOrdenPago()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarOrdenPago()
    End Sub

    Private Sub txtProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNroComprobante.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarOrdenPago()

        End If
    End Sub

    Private Sub dgVentas_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgVentas.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgVentas_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgVentas.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        frmVentas = New frmOrdenPago()
        frmVentas.codOrdenPago = 0
        frmVentas.ShowDialog()
        CargarOrdenPago()
    End Sub

    Private m_frmBusCliente As frmProveedorBuscador
    Public Property frmBusCliente() As frmProveedorBuscador
        Get
            Return m_frmBusCliente
        End Get
        Set(ByVal value As frmProveedorBuscador)
            m_frmBusCliente = value
        End Set
    End Property

    Private Sub btnBuscProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscProveedor.Click
        frmBusCliente = New frmProveedorBuscador
        frmBusCliente.ShowDialog()
        txtProveedor.Text = frmProveedorBuscador.ProveedorDesc
        codProveedor = frmProveedorBuscador.codProveedor
        If codProveedor > 0 Then
            CargarOrdenPago()
        End If
    End Sub

    Private Sub btnCancelProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelProveedor.Click
        codProveedor = 0
        txtProveedor.Text = ""
        CargarOrdenPago()
    End Sub

    Private m_frmReporte As frmReportes
    Public Property frmRpts() As frmReportes
        Get
            Return m_frmReporte
        End Get
        Set(ByVal value As frmReportes)
            m_frmReporte = value
        End Set
    End Property

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If frmRpts Is Nothing Then
            frmRpts = New frmReportes
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "ORDENPAGO"
            frmRpts.datos_Reporte = CType(dgVentas.DataSource, DataTable).Copy
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New frmReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "ORDENPAGO"
                frmRpts.datos_Reporte = CType(dgVentas.DataSource, DataTable).Copy
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "ORDENPAGO"
                frmRpts.datos_Reporte = CType(dgVentas.DataSource, DataTable).Copy
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If

    End Sub

End Class